/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.ArrayList;
import model.hitoModel;
import model.roadModel;
import model.treeModel;
import store.layersStore;
import store.treeStore;
import view.layerView;
import view.menuView;
import view.treeView;

/**
 *
 * @author JorgeLuis
 */
public class mainController {
    private mainWindowsController           windowsController   = new mainWindowsController();
    private menuController                  menuController      = new menuController();
    private treeController                  treeController      = new treeController();
    private editElementController           editElement         = new editElementController();
    private layersStore                     layerStore          = new layersStore();
    private int id = 0;

    public mainController() { 
        
        layerStore.newLayer(this.assignId(), -1);
        layerStore.getSelectLayer().mainController(this);
        
        menuController.mainController(this);
        treeController.mainController(this);
        editElement.mainController(this);
        
        treeController.startTree("Javeriana", layerStore.getSelectLayer().getId());
        
        menuView menuView           = menuController.getView();
        treeView treeView           = treeController.getView();
        layerView layerView         = layerStore.getSelectLayer().getView();    
        menuView.setSize(61, 92);
        treeView.setSize(200, 92);
        layerView.setSize(500, 500);
        windowsController.getView().setPanelCenter(layerView);
        windowsController.getView().setPanelLeft(menuView);
        windowsController.getView().setPanelRight(treeView);
        
               
        windowsController.show();
    }
    
    public int assignId(){
        this.id +=1;
        return this.id;
    }

    public void setState(String event) {
        layerStore.getSelectLayer().setState(event);
    }

    public void editHito(hitoModel hitoEdit) {
        this.editElement.setElement(hitoEdit);
        this.editElement.show();
    }
    
    public void editRoad(roadModel roadEdit) {
        this.editElement.setElement(roadEdit);
        this.editElement.show();
    }

    public layerController getLayerSelect() {
        return layerStore.getSelectLayer();
    }

    public void editElement(hitoModel senHito) {        
        this.layerStore.getSelectLayer().editElement(senHito);        
        this.treeController.editTree(senHito, layerStore.getSelectLayer().getId());
    }  
    
    public void editElement(roadModel senRoad) {        
        this.layerStore.getSelectLayer().editElement(senRoad);
    }

    public void changeLayer(int id) {
        // buscamos el layer con el id 
        int position = this.layerStore.findLayerIndex(id);
        this.layerStore.selectLayer(position);
        System.out.println("Se cambio la capa "+position+" "+id);
        this.windowsController.getView().setPanelCenter(this.layerStore.getSelectLayer().getView());
    }

    public int newLayer(int parentId) {
        int i = assignId();
        this.layerStore.newLayer(i, parentId);
        return i;
    }

    public void deleteEdifice(int id) {
       /*
           Buscamos los pisos que tiene el edificio
        */
        
       if(this.layerStore.getSelectLayer().getId() == id){
           
           this.windowsController.getView().setPanelCenter(null);
       } 
       
       this.layerStore.deleteLayer(id);
       
    }
    
}
